package com.origin.src;

import org.springframework.context.support.GenericApplicationContext;

import java.util.HashSet;

public class ApplicationTool {

    static int m = 1;

    public static void printBean(GenericApplicationContext applicationContext) {
        HashSet<String> set = new HashSet<>();
        applicationContext.getBeanFactory().getBeanNamesIterator().forEachRemaining(name -> {
            m = Math.max(m, name.length());
            set.add(name);
        });

        for (String s : applicationContext.getBeanDefinitionNames()) {
            m = Math.max(m, s.length());
            set.add(s);
        }

        m = m / 5 * 3;

        StringBuilder sb = new StringBuilder("\033[31m" + String.format("%-" + (m) + "s", "Name"));

        sb.append("Definition\tBean");

        sb.append("\033[m\n");
        set.forEach(name -> {
            if (applicationContext.containsBean(name)) {
                String n = applicationContext.getBean(name).getClass().getName();
                if (n.startsWith("org.spring") || n.startsWith("java")) {
                    return;
                }
            }
            sb
//                    .append(name)
//                    .append("\n")
                    .append(String.format("%-" + (m) + "s", name))
                    .append(applicationContext.containsBeanDefinition(name)).append("\t\t\t")
                    .append(applicationContext.containsBean(name)).append("\n");
        });

        System.out.println(sb);

    }

    public static void printClass(Object obj) {
        System.out.println("构造方法：" + obj.getClass().getName());
    }

}
